/*
 * NormalFitness.java
 *
 * Created on den 30 mars 2007, 23:30
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package grex.fitnessfunctions;

import grex.GP;
import grex.Options;
import grex.Data.Prediction;
import grex.Nodes.GeneException;

import java.io.Serializable;

/**
 *
 * @author rik
 */
public class ProbFitness implements IFitnessFunction, Serializable {

    /** Creates a new instance of NormalFitness */
    public ProbFitness() {
    }

    public double calcFitness(GP gp) throws GeneException {
        Options options = gp.getOptions();
        gp.train();
        int[] probOrder = options.getProbOrder();
        int nrOfClasses = probOrder.length;

        double trainFitness = 0;
        int E = 0;
        int nrOfAttributes = gp.getPcTrain().values().iterator().next().getInstance().length;
        double diff;
        double target;
        for (Prediction p : gp.getPcTrain().values()) {
            double[] probs = p.getProbs();
            double bri = 0;
          //  if(p.getPrediction()==p.getTargetValue())
          //          trainFitness++;
            for (int i = 0; i < probs.length; i++) {

                target = p.getInstance()[nrOfAttributes-nrOfClasses-1+i];//probOrder[i]];
                diff = probs[i] - target;
                bri +=(1- Math.pow(diff, 2))/nrOfClasses;
            }//*/
            trainFitness+=bri;
        }
        trainFitness = 1.0 - trainFitness / gp.getPcTrain().values().size();
        
        return 100*trainFitness  + 100*trainFitness * options.getPUNISHMENT_FOR_LENGTH()*gp.getNrOfNodes();
//        return (trainFitness / gp.getPcTrain().values().size() - options.getPUNISHMENT_FOR_LENGTH()*gp.getLength()/100)*100;

    }
}
